package com.robot.admin.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.robot.admin.dao.ProvinceDao;
import com.robot.admin.pojo.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

/**
 * province服务层
 *
 * @author lin
 */
@Service
public class ProvinceService {
    @Autowired
    private ProvinceDao provinceDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Province> findAll() {
        return provinceDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Province> findSearch(Map whereMap, int page, int size) {
        Specification<Province> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return provinceDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Province> findSearch(Map whereMap) {
        Specification<Province> specification = createSpecification(whereMap);
        return provinceDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param code
     * @return
     */
    public Province findById(String code) {
        return provinceDao.findById(code).get();
    }

    /**
     * 增加
     *
     * @param province
     */
    public void add(Province province) {
        //雪花分布式ID生成器
        province.setCode(idWorker.nextId() + "");
        provinceDao.save(province);
    }

    /**
     * 修改
     *
     * @param province
     */
    public void update(Province province) {
        provinceDao.save(province);
    }

    /**
     * 删除
     *
     * @param code
     */
    public void deleteById(String code) {
        provinceDao.deleteById(code);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Province> createSpecification(Map searchMap) {
        return new Specification<Province>() {
            @Override
            public Predicate toPredicate(Root<Province> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编码
                String code = "code";
                if (searchMap.get(code) != null && !"".equals(searchMap.get(code))) {
                    predicateList.add(cb.like(root.get(code).as(String.class), "%" + (String) searchMap.get(code) + "%"));
                }
                // 省名
                String name = "name";
                if (searchMap.get(name) != null && !"".equals(searchMap.get(name))) {
                    predicateList.add(cb.like(root.get(name).as(String.class), "%" + (String) searchMap.get(name) + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 根据code查找province
     *
     * @param code
     * @return
     */
    public Province findByCode(String code) {
        return provinceDao.findByCode(code);
    }

    /**
     * 根据name查找province
     *
     * @param name
     * @return
     */
    public Province findByName(String name) {
        return provinceDao.findByName(name);
    }
}
